<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){	
	require_once('include/ClaimsAPIService.php');
	require_once('include/EligibilityAPIServicePort.php');
	require_once('include/functions.php');
	require_once('config.php');

	$claimsAPIServiceClient = new ClaimsAPIService($claimsAPIServiceWSDL,array());
	$eligibilityAPIServicePortClient = new EligibilityAPIServicePort($eligibilityAPIServicePortWSDL, array());
	$eligibilityAPIServicePortClient->__setLocation($eligibilityAPIServicePortLocation);

	/* build request ticket info */
	$requestTicketInfo = new RequestTicket();
	$requestTicketInfo->username = $username;
	$requestTicketInfo->password = $password;

	/* request a ticket */
	$requestTicketResponse = $claimsAPIServiceClient->RequestTicket($requestTicketInfo);

	$authTicket = $requestTicketResponse->RequestTicketResult;
	$error = new ErrorMessage();
	/* check for error */
	if(strlen($authTicket) == 1){
		switch($authTicket){
			/*
			 *  "G” – User account could not be found
			 *	“H” – User account is not activ
			 *	“I” – User account has expired
			 *	“B” – User account is locked out
			 *	“E” – User account attempted login with the wrong password
			 */
			case 'G':
				$error->Code = 'G';
				$error->Text = 'User account could not be found.';
				break;
			case 'H':
				$error->Code = 'H';
				$error->Text = 'User account is not active.';
				break;
			case 'I':
				$error->Code = 'I';
				$error->Text = 'User account has expired.';
				break;
			case 'B':
				$error->Code = 'B';
				$error->Text = 'User account is locked out.';
				break;
			case 'E':
				$error->Code = 'E';
				$error->Text = 'User account attempted login with the wrong password';
				break;
			default:
				$error->Code = 'U';
				$error->Text = 'Unknown error occurred.';
		}
		header('Location: ' . $_SERVER['REQUEST_URI'] . "?error_message=" . urlencode($error->Text));
						
	}

	/* we have our ticket, make a call to get BatchCertEligibility */
	$batchCertEligibilityGetInfo = new BatchCertEligibilityGet();
	$batchCertEligibilityGetInfo->AuthTicket = $authTicket;
	/**
	 * FIXME: don't hard code the RecType
	 *
	 */
	$batchCertEligibilityGetInfo->RecTyp = 'Both';

	$divisionSelection = new DivisionSelection();
	$divisionSelection->Certs->Cert->AutobenCertNum = $_POST['AutobenCertNum'];

	$groupSelection = new GroupSelection();
	$groupSelection->Divs->DivSelection = $divisionSelection;
	$groupSelection->TPPolicyNum = $_POST['TPPolicyNum'];
	$batchCertEligibilityGetInfo->Groups->GroupSelection = $groupSelection;

	$batchCertEligibilityGetInfo->ExpAll = false;
	$batchCertEligibilityGetInfo->RqstRefGUID = uuid("");
	$batchCertEligibilityGetInfo->FrmDt = $_POST['FrDt'];
	$batchCertEligibilityGetInfo->ToDt = $_POST['ToDt'];
	$batchCertEligibilityGetInfo->Ver = '1.0';
	$batchCertEligibilityGetResponse = $eligibilityAPIServicePortClient->BatchCertEligibilityGet($batchCertEligibilityGetInfo);

	if($batchCertEligibilityGetResponse->Successful != true){
		$error->Code = 'U';
		$error->Text = $batchCertEligibilityGetResponse->ErrorMessage;
		header('Location: ' . $_SERVER['REQUEST_URI'] . "?error_message=" . urlencode($error->Text));
	}else{
		$error->Code = '';
		$error->Text = '';	
	}
}
	?>
<div align="left" style="padding-left:10cm;">
<form method="post" name="formname" action="info.php">AutobenCertNum:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input
	type="text" name="AutobenCertNum" size="30"> <br />
<br />
TPPolicyNum:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="TPPolicyNum" size="30" /> <br />
<br />

From date:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="FrDt" size="15" /> <br />
<br />

To date:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="ToDt" size="15" /> <br />
<br />

<input type="submit" name="submitbtn" value="Submit"/>
<br /><br />
<?php if(isset($_GET['error_message'] )) echo "<b>" . $_GET['error_message'] . "</b>";?>
</form>
</div>
<?php 
		if($_SERVER['REQUEST_METHOD'] == 'POST' && $error->Code == ''){
			//print_r($batchCertEligibilityGetResponse);
			$cert = $batchCertEligibilityGetResponse->EligibilityInfo->Grp->Divs->Div->Certs->Cert;
			echo "<table border='1'>";
			echo "<tr><th colspan='2'>Employee Information</th></tr>";
			echo "<tr><td>IsNew</td><td>" . $cert->IsNew . "</td></tr>";
			echo "<tr><td>IsChgDiv</td><td>" . $cert->IsChgDiv . "</td></tr>";
			echo "<tr><td>IsChgDiv</td><td>" . $cert->IsChgDiv . "</td></tr>";
			echo "<tr><td>FN</td><td>" . $cert->FN . "</td></tr>";	
			echo "<tr><td>LN</td><td>" . $cert->LN . "</td></tr>";
			echo "<tr><td>MidInit</td><td>" . $cert->MidInit . "</td></tr>";
			echo "<tr><td>Birth</td><td>" . $cert->Birth . "</td></tr>";
			echo "<tr><td>Addr</td><td>Addr1: " . $cert->Addr->Addr1 . "<br />" 
									. "Addr2: " . $cert->Addr->Addr2 . "<br />"
									. "City: " . $cert->Addr->City . "<br />"
									. "Prov: " . $cert->Addr->Prov . "<br />"
									. "PostCd: " . $cert->Addr->PostCd . "<br />"
									. "Country: " . $cert->Addr->Country . "<br />"
									. "</td></tr>";
			echo "<tr><td>Gender</td><td>" . $cert->Gender . "</td></tr>";
			echo "<tr><td>MaritlStat</td><td>" . $cert->MaritlStat . "</td></tr>";
			echo "<tr><td>CovEffDate</td><td>" . $cert->CovEffDate . "</td></tr>";
			echo "<tr><td>CovTermDate</td><td>" . $cert->CovTermDate . "</td></tr>";
			echo "<tr><td>Lang</td><td>" . $cert->Lang . "</td></tr>";
			echo "<tr><td>Health</td><td>" . $cert->Health->Code . "/" . $cert->Health->Stat . "</td></tr>";		
			echo "<tr><td>Dental</td><td>" . $cert->Dental->Code . "/" . $cert->Dental->Stat . "</td></tr>";
			echo "<tr><td>Vision</td><td>" . $cert->Vision->Code . "/" . $cert->Vision->Stat . "</td></tr>";
			echo "<tr><td>EDrugEffDate</td><td>" . $cert->EDrugEffDate . "</td></tr>";
			echo "<tr><td>EDrugTermDate</td><td>" . $cert->EDrugTermDate . "</td></tr>"; 
			echo "<tr><td>Email</td><td>" . $cert->Email . "</td></tr>";
			echo "<tr><td>Phone</td><td>" . $cert->Phone . "</td></tr>";
			echo "<tr><td>Salary</td><td>" . $cert->Salary . "</td></tr>";
			echo "<tr><td>WaitingPeriod</td><td>" . $cert->WaitingPeriod . "</td></tr>";
			echo "<tr><td>Indian</td><td>" . $cert->Indian . "</td></tr>";
			echo "<tr><td>COBCd</td><td>" . $cert->COBCd . "</td></tr>";
			echo "<tr><td>SpouseCarrier</td><td>" . $cert->SpouseCarrier . "</td></tr>";
			echo "<tr><td>SpousePolicyNo</td><td>" . $cert->SpousePolicyNo . "</td></tr>";
			echo "<tr><td>ApplyHcsa</td><td>" . $cert->ApplyHcsa . "</td></tr>";
			echo "<tr><td>Hcsa</td><td>" . $cert->Hcsa . "</td></tr>";
			echo "<tr><td>CPEffDate</td><td>" . $cert->CPEffDate . "</td></tr>";
			echo "<tr><td>WI</td><td>" . $cert->WI . "</td></tr>";
			echo "<tr><td>ChgEffDate</td><td>" . $cert->ChgEffDate . "</td></tr>";
			echo "<tr><td>SurvivorBenefitEffDate</td><td>" . $cert->SurvivorBenefitEffDate . "</td></tr>";
			echo "<tr><td>Trustee</td><td>" . $cert->Trustee . "</td></tr>";
			echo "<tr><td>NumOfDependants</td><td>" . count($cert->Dependants->Dependant);		
			echo "</table>";	
			echo "<br />";
			echo "<table border='1'>";
			echo "<tr><th colspan='19'>Dependant information</th></tr>";
			echo "<tr><td>IsNew</td>"
				. "<td>RelCd</td>"
				. "<td>FN</td>"
				. "<td>LN</td>"
				. "<td>MidInit</td>"
				. "<td>Birth</td>"
				. "<td>Addr</td>"
				. "<td>Gender</td>"
				. "<td>CovEffDater</td>"
				. "<td>CovTermDate</td>"
				. "<td>EDrugEffDate</td>"
				. "<td>EDrugTermDate</td>"
				. "<td>Email</td>"
				. "<td>Phone</td>"
				. "<td>Student</td>"
				. "<td>Health</td>"
				. "<td>Dental</td>"
				. "<td>Vision</td>"
				. "<td>ChgEffDate</td>"
				. "</tr>";
			foreach($cert->Dependants->Dependant as $key => $dependant){
				echo "<tr><td>" . $dependant->IsNew . "</td>"
				. "<td>" . $dependant->RelCd . "</td>"
				. "<td nowrap='nowrap'>" . $dependant->FN . "</td>"
				. "<td nowrap='nowrap'>" . $dependant->LN . "</td>"
				. "<td>" . $dependant->MidInit . "</td>"
				. "<td>" . $dependant->Birth . "</td>"
				. "<td nowrap='nowrap'>" . "Addr1: " . $dependant->Addr->Addr1 . "|" 
									. "Addr2: " . $dependant->Addr->Addr2 . "|"
									. "City: " . $dependant->Addr->City . "|"
									. "Prov: " . $dependant->Addr->Prov . "|"
									. "PostCd: " . $dependant->Addr->PostCd . "|"
									. "Country: " . $dependant->Addr->Country . "|"
									. "</td>"
				. "<td>" . $dependant->Gender . "</td>"
				. "<td>" . $dependant->CovEffDate . "</td>"
				. "<td>" . $dependant->CovTermDate . "</td>"
				. "<td>" . $dependant->EDrugEffDate . "</td>"
				. "<td>" . $dependant->EDrugTermDate . "</td>"
				. "<td>" . $dependant->Email . "</td>"
				. "<td>" . $dependant->Phone . "</td>"
				. "<td>" . $dependant->Student . "</td>"
				. "<td>" . $dependant->Health . "</td>"
				. "<td>" . $dependant->Dental . "</td>"
				. "<td>" . $dependant->Vision . "</td>"
				. "<td>" . $dependant->ChgEffDate . "</td>"
				. "</tr>";
			}
			echo "</table>";
						
		}
?>




